
const app = getApp();
const req = require("../../utils/http.js");
Page({

  data: {
    BackgroundImg: 'https://syl-estate.oss-cn-shenzhen.aliyuncs.com/template/645505222748631502.jpg',
    userInfo:'',
    loginShow:'',//弹出登录页
    popPhone:false,
    logText: true,
    setHidden: '',
    homeAt: '/imgs/index/home.png',
    my: '/imgs/index/myAt.png'
  },


  //获取用户头像
  bindGetUserInfo: function (e) {
    let t = this
    if (e.detail.userInfo) {
      app.globalData.userInfo = e.detail.userInfo
      t.setData({
        userInfo: e.detail.userInfo,
        loginShow:false,
        encryptedData: e.detail.encryptedData,
        ivStr: e.detail.iv,
      })
      t.saveUserinfo()

    } else {
      wx.showToast({
        title: "为了您更好的体验,请先同意授权",
        icon: 'none',
        duration: 2000
      });
    }
  },
  // ---点击未登录按钮---
  notLanding() {
    this.setData({
      loginShow: false  //授权手机号隐藏
    })
    wx.navigateTo({
      url: '/pages/index/index',
    })
  },


  // _-手机号授权--
  getPhoneNumber: function (e) {
    console.log(e)
    let t = this
    t.setData({
      popPhone: false
    })
    if (e.detail.iv) {
     
      const params = {
        encryptedData: e.detail.encryptedData,
        ivStr: e.detail.iv,
      }
      req._objPost("/coupon/wxUser/savePhone",'POST',params).then(res => {
        if (res.data.code == 200) {
          wx.showToast({
            title: res.data.message,
            icon:'none',
            duration:1500
          })
          t.setData({
            popPhone:false,
            logText:false,
          })

        }else{
          wx.showToast({
            title: res.message,
            icon: 'none',
            duration: 1500
          })
          }
      })
    } else {
      console.log("用户按了拒绝按钮");
      wx.showToast({
        title: "为了您更好的体验,请先同意授权",
        icon: 'none',
        duration: 1600,  
      });
     
    }
  },
  notPhone(){
    let t = this
    t.setData({
      popPhone:false
    })
  },
  // --保存用户信息
  saveUserinfo(){
    let t = this
    let params = {
      encryptedData: t.data.encryptedData,
      ivStr: t.data.ivStr
    }
    if (t.data.encryptedData){
      req._objPost('/coupon/wxUser/saveUserinfo',"POST",params).then(res=>{
        console.log('保存用户信息',res)
      })
    }else{
      console.log('请先授权手机号')
    }
  },
  //会员登录（获取用户手机号）
  memberClik(){
    this.setData({
      popPhone:true
      })
  },

  // 查询当前用户
  isUserinfo(){
    let t = this
    let params={}
    req.wxReq('/coupon/wxUser/isUserinfo',"GET",params).then(res=>{
      console.log('查询用户信息',res)
      if(res.data.code==200){
        t.setData({
          userInfo: res.data.result,
          loginShow:false
        })
      }else{
        wx.showToast({
          title: res.data.message,
          icon:'none',
          duration:1500
        })
        t.setData({
          loginShow: true
        })
      }
   
    })
  },
  getInput(e) {
    this.setData({
      value: e.detail.value
    })
  },
  // 绑定团队
  clikGroup() {
    let t = this
    t.setData({
      GroupShow: true,
      GroupName: '绑定团队',
      useName: '手机号',
      setHidden: true
    })
  },
  //绑定团队/退款 取消
  abolish() {
    let t = this
    t.setData({
      GroupShow: false
    })
  },
  // --绑定团队 /退款 确定---
  confirm() {
    let t = this
    let value = t.data.value
    let setHidden = this.data.setHidden
    console.log(setHidden)
    if (setHidden == true) { //绑定团队
      if (!(/^1[3456789]\d{9}$/.test(value))) {
        wx.showToast({
          title: '手机号码有误，请重填！',
          icon: 'none',
          duration: 1500
        })
        return false;
      }
      let params = {
        mobile: value,
      }
      req.wxReq("/coupon/wxUser/reset","POST",params).then(res => {
        if (res.data.code == 200) {
          wx.showToast({
            title: res.data.message,
            icon: 'none',
            duration: 1000
          })
          t.setInfo()
          setTimeout(() => {
            t.setData({
              GroupShow: false
            })
          }, 1400)

        } else {
          wx.showToast({
            title: res.data.message,
            icon: 'none',
            duration: 1000
          })
        }
      })
    } else { //手动退款
      const params = {
        orderId: value
      }

      req._objPost('/coupon/wxProduct/refund', "POST", params).then(res => {
        if (res.data.code == 200) {
          wx.showModal({
            content: res.data.result,
            showCancel: false,
            success(res) {
              if (res.confirm) {
                t.setData({
                  GroupShow: false
                })
              }
            }
          })

        } else {
          wx.showToast({
            title: res.data.message,
            icon: 'none',
            duration: 1000
          })
        }
      })
    }
    // return

  },

  goTicket() {
    wx.navigateTo({
      url: '../ticket/index'
    })
  },
  goScore: function (e) {
      wx.navigateTo({
        url: '../score/index'
      })

  },
  addressClik(){
    wx.navigateTo({
      url: '../address/index',
    })
  },
  // 手动退款
  reimburse() {
    let t = this
    wx.showModal({
      title: '提示',
      content: '退款需扣掉两个积分哦',
      success(res) {
        if (res.confirm) {
          t.setData({
            setHidden: false,
            GroupName: '手动退款',
            useName: '订单号',
            GroupShow: true,
          })

        } else if (res.cancel) {
          t.setData({
            GroupShow: false,
          })
        }
      }
    })

  },
  //已付款
  getPayment: function () {
    let type = 1
    wx.navigateTo({
      url: '../order/index?type=' + type
    })
  },
  tabBar() {
    req.wxReq('/coupon/wxUser/selectMiniMenuConfigByCompanyId', "GET").then(res => {
      console.log('tabBar', res)
      if (res.data.code == 200) {
        this.setData({
          tabBarList: res.data.result
        })
      }

    })
  },

  tabBarClick(e) {
    let url = e.detail.url
    console.log(url)
    wx.redirectTo({
      url: url,
    })

  },
  //已完成
  getEndOrder: function () {
    let type = 2
    wx.navigateTo({
      url: '../order/index?type=' + type
    })
  },
  //退款
  refund: function () {
    let type = 3
    wx.navigateTo({
      url: '../order/index?type=' + type
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let t = this
  
    t.isUserinfo()
    t.tabBar()
  
    wx.setNavigationBarTitle({
      title: wx.getStorageSync('appName'),
    })

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})
